package baidu.reader.doc.model
{
    import baidu.lib.serialization.*;
    import baidu.reader.common.model.*;
    import baidu.ui.managers.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class DocIdInfoProxy extends EventDispatcher
    {
        private var urlLoader:URLLoader;
        private var _totalPage:int;
        private var _docid:String;
        private var _locked:Boolean = false;

        public function DocIdInfoProxy(param1:IEventDispatcher = null)
        {
            this.urlLoader = new URLLoader();
            super(param1);
            this.urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
            this.urlLoader.addEventListener(Event.COMPLETE, this.dataCompleteHandler);
            this.urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this.errorHandler);
            this.urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.errorHandler);
            return;
        }

        private function errorHandler(event:Event) : void
        {
            dispatchEvent(new Event(ErrorEvent.ERROR));
            return;
        }

        private function dataCompleteHandler(event:Event) : void
        {
            var docInfo:Object;
            var event:* = event;
            var rawByteArray:* = this.urlLoader.data;
            rawByteArray.endian = Endian.LITTLE_ENDIAN;
            var docInfoBinaryData:* = new ByteArray();
            docInfoBinaryData.endian = Endian.LITTLE_ENDIAN;
            rawByteArray.position = 0;
            if (rawByteArray.bytesAvailable > 106 + rawByteArray.position)
            {
                rawByteArray.readBytes(docInfoBinaryData, 0, 106);
            }
            else
            {
                trace("binaryData.bytesAvailable: " + rawByteArray.bytesAvailable);
                trace("��ݸ�ʽ�����ļ�̫С");
                dispatchEvent(new Event(ErrorEvent.ERROR));
                return;
            }
            var docInfoStr:* = docInfoBinaryData.readMultiByte(docInfoBinaryData.bytesAvailable, "utf-8");
            try
            {
                docInfo = JSON.decode(docInfoStr);
            }
            catch (e:Error)
            {
                trace("�����ĵ���Ϣʧ��");
            }
            this._totalPage = int(docInfo["totalPage"]);
            dispatchEvent(event);
            return;
        }

        public function getInfo(param1:String) : void
        {
            var _loc_2:* = (InstanceManager.createSingletonInstance(AppConfigModel) as AppConfigModel).getContentURL(param1, ("pn=" + 1) + "&rn=" + 1);
            this.urlLoader.load(new URLRequest(_loc_2));
            this._docid = param1;
            return;
        }

        public function get totalPage() : int
        {
            return this._totalPage;
        }

        public function get docid() : String
        {
            return this._docid;
        }

    }
}
